import UIKit

/* 属性 */

// 1
struct Circle {
    let pi: Double = 3.14159
    var radius: Double = 0
    
    var aera: Double {
        get {
            pi * radius * radius
        }
        set {
            radius = sqrt(newValue / pi)
        }
    }
}

var smallCircle = Circle(radius: 6)
print(smallCircle.aera)

var bigCircle = Circle()
bigCircle.aera = 10000
print(bigCircle.radius)

// 2
struct Car {
    let brand: String
    var licensePlate: String
    var ownerName: String {
        willSet {
            print("Owner will change")
        }
        didSet {
            print("Old Owner:\(oldValue) New Owner:\(ownerName)")
        }
    }
}

var myCar = Car(brand: "Volkswagen", licensePlate: "123456", ownerName: "Owner1")
myCar.ownerName = "Owner2"

// 3
struct GamePlayer {
    static var allHighestScore: Int = 0
    
    let id: String
    let name: String
    
    var nowGame: Int = 1
    var nowScore: Int
    
    var allScore: Int {
        switch nowGame {
        case 1: nowScore
        case 2: 1000 + nowGame
        case 3: 10000 + nowGame
        case 4: 20000 + nowGame
        case 5: 30000 + nowGame
        case 6: 40000 + nowGame
        default: 0
        }
    }
}

var player1 = GamePlayer(id: "1", name: "player1", nowScore: 876)
var player2 = GamePlayer(id: "2", name: "player2", nowScore: 6690)

GamePlayer.allHighestScore = (player1.allScore > GamePlayer.allHighestScore) ? player1.allScore : GamePlayer.allHighestScore
GamePlayer.allHighestScore = (player2.allScore > GamePlayer.allHighestScore) ? player2.allScore : GamePlayer.allHighestScore

print("player1:\(player1.allScore) player2:\(player2.allScore)")
print("all highest score:\(GamePlayer.allHighestScore)")


/* 方法 */
// 1
struct basketBallPlayer {
    let name: String
    
    var threeScore: Int = 0
    var twoScore: Int = 0
    
    var score: Int {
        threeScore * 3 + twoScore * 2
    }
}

// 2
struct basketBallTeam {
    let name: String
    
    var members = [basketBallPlayer]()
    
    var score: Int {
        var scoreCount = 0
        for player in members {
            scoreCount = scoreCount + player.score
        }
        return scoreCount
    }
    
    init(name: String) {
        self.name = name
    }
    
    mutating func addMember(newMember: basketBallPlayer) {
        members.append(newMember)
    }
}

// 3
let basketBallPlayer1 = basketBallPlayer(name: "player1", threeScore: 1, twoScore: 2)
let basketBallPlayer2 = basketBallPlayer(name: "player2", threeScore: 3, twoScore: 4)
let basketBallPlayer3 = basketBallPlayer(name: "player3", threeScore: 5, twoScore: 6)

var basketBallTeam1 = basketBallTeam(name: "team1")
basketBallTeam1.addMember(newMember: basketBallPlayer1)
basketBallTeam1.addMember(newMember: basketBallPlayer2)
basketBallTeam1.addMember(newMember: basketBallPlayer3)

print("Team score:\(basketBallTeam1.score)")
for player in basketBallTeam1.members {
    print("Name:\(player.name) 3 Score:\(player.threeScore) 2 Score:\(player.twoScore)")
}

// 4
struct ToolSet {
    static func taxPayment(salary: Double) -> Double{
        var tax: Double = 0
        var remain: Double = salary
        
        if(salary > 3000) {
            tax += 3000 * 0.03
            remain -= 3000
            if(salary > 12000) {
                tax += (12000 - 3000) * 0.10
                remain -= (12000 - 3000)
                if(salary > 25000) {
                    tax += (25000 - 12000) * 0.20
                    remain -= (25000 - 12000)
                    
                    tax += remain * 0.30
                } else {
                    tax += remain * 0.20
                }
            } else {
                tax += remain * 0.10
            }
        } else {
            tax += remain * 0.03
        }
        return tax
    }
}

ToolSet.taxPayment(salary: 25000)

// 5
extension basketBallPlayer {
    mutating func gainScore(score: Int) {
        if score == 2 {
            twoScore += 1
        } else if score == 3 {
            threeScore += 1
        }
    }
    
    func description() {
        print("Name:\(name) 3 Score:\(threeScore) 2 Score:\(twoScore) Score:\(score)")
    }
}

// 6
extension basketBallTeam {
    subscript(index: Int) -> basketBallPlayer {
        get {
            members[index]
        }
        set {
            if index >= members.count {
                members.append(newValue)
            } else {
                members[index] = newValue
            }
        }
    }
}

basketBallTeam1[0].description()
basketBallTeam1[1].threeScore = 4
basketBallTeam1[2].gainScore(score: 2)

let basketBallPlayer4 = basketBallPlayer(name: "player3", threeScore: 7, twoScore: 8)
basketBallTeam1[3] = basketBallPlayer4
basketBallTeam1[3].description()
// 可以
// 在set中进行一定的判断防止越界即可
